﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MySqlCode.Handler
{
    public class Generate
    {
        /// <summary>
        /// 生成类代码
        /// </summary>
        public static StringBuilder ClassCode(List<Model.Column> cols, string tbname, string packname)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine($"package {packname};");
            sb.AppendLine($"import java.util.Date;");
            sb.AppendLine("");
            sb.AppendLine($"public class {tbname.PascalNaming()}Model {{");
            foreach (var cl in cols)
            {
                string template = @"
    /** [comment] */
    private [type] [name];
    /**
     * 获取对象的[comment]属性值
     * @return [comment]属性值
     */
    public [type] [getname]() {
        return this.[name];
    }
    /**
     * 设置对象的[comment]属性值
     * @param _[name] 需要设置的值
     */
    public void [setname]([type] _[name]) {
        this.[name] = _[name];
    }";
                template = template.Replace("[type]", cl.Type);
                template = template.Replace("[name]", cl.Field.HumpNaming());
                template = template.Replace("[comment]", cl.Comment);
                template = template.Replace("[getname]", cl.Field.GetPropertyName());
                template = template.Replace("[setname]", cl.Field.SetPropertyName());
                sb.AppendLine(template);
            }
            sb.AppendLine("}");
            return sb;
        }
        /// <summary>
        /// 生成有ID拼接的列名
        /// </summary>
        public static string FieldYesId(List<Model.Column> cols)
        {
            string field = "";
            foreach (var cl in cols)
            {
                field += $"{cl.Field.ToLower()},";
            }
            field = field.Substring(0, field.Length - 1);
            return field;
        }
        /// <summary>
        /// 生成没有ID拼接的列名
        /// </summary>
        public static string FieldNoId(List<Model.Column> cols)
        {
            string field = "";
            foreach (var cl in cols)
            {
                if (cl.Field != "id")
                    field += $"{cl.Field.ToLower()},";
            }
            field = field.Substring(0, field.Length - 1);
            return field;
        }
        /// <summary>
        /// 生成更新的SQL代码
        /// </summary>
        public static string UpdateSetValues(List<Model.Column> cols)
        {
            string field = "";
            foreach (var cl in cols)
            {
                field += $"{cl.Field.ToLower()}=?,";
            }
            field = field.Substring(0, field.Length - 1);
            return field;
        }
        /// <summary>
        /// 生成更新的SQL代码
        /// </summary>
        public static string UpdateSetParams(List<Model.Column> cols)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 1; i <= cols.Count; i++)
            {
                if (cols[i - 1].Type.ToLower() == "date")
                    sb.AppendLine($"			db.psm.{cols[i - 1].Type.SetParamType()}({i}, new Timestamp(model.get{cols[i - 1].Field.PascalNaming()}().getTime()));");
                else
                    sb.AppendLine($"			db.psm.{cols[i - 1].Type.SetParamType()}({i}, model.get{cols[i - 1].Field.PascalNaming()}());");
            }
            sb.AppendLine($"			db.psm.setInt({cols.Count + 1}, model.getId());");
            return sb.ToString();
        }
        /// <summary>
        /// 生成获取对象的代码
        /// </summary>
        public static string SetModel(List<Model.Column> cols)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 1; i <= cols.Count; i++)
            {
                sb.AppendLine($"				model.set{cols[i - 1].Field.PascalNaming()}(rs.{cols[i - 1].Type.GetParamType()}({i}));");
            }
            return sb.ToString();
        }
        /// <summary>
        /// 生成插入语句所需参数量
        /// </summary>
        public static string InsertParams(List<Model.Column> cols)
        {
            var field = "";
            for (int i = 0; i < cols.Count; i++)
            {
                if (cols[i].Field.ToLower() != "id")
                    field += "?,";
            }
            field = field.Substring(0, field.Length - 1);
            return field;
        }
        /// <summary>
        /// 生成插入的SQL代码
        /// </summary>
        public static string InsertSetParams(List<Model.Column> cols)
        {
            StringBuilder sb = new StringBuilder();
            int index = 1;
            foreach (var cl in cols)
            {
                if (cl.Field.ToLower() != "id")
                {
                    if (cl.Type.ToLower() == "date")
                        sb.AppendLine($"			db.psm.{cl.Type.SetParamType()}({index}, new Timestamp(model.get{cl.Field.PascalNaming()}().getTime()));");
                    else
                        sb.AppendLine($"			db.psm.{cl.Type.SetParamType()}({index}, model.get{cl.Field.PascalNaming()}());");
                    index++;
                }
            }
            return sb.ToString();
        }
    }
}
